package com.zmn.plat.admin.controller.orderconfig.documentarycontent;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zmn.common.dto2.ResponseDTO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.plat.model.vo.orderconfig.documentarycontent.DocumentaryContentVO;

/**
 * 类描述:跟单内容修改控制器
 *
 * @author linfeng
 * @since 2019/10/21/14:47
 */
@Controller
@RequestMapping(value = "/orderconfig/documentarycontent/")
public class DocumentaryContentModifyController extends DocumentaryContentBaseController {
    private Logger logger = LoggerFactory.getLogger(DocumentaryContentListController.class);

    @RequestMapping(value = "updateStatus")
    @ResponseBody
    @RequiresPermissions("/orderconfig/documentarycontent/updateStatus.action")
    public ResponseDTO updateStatus(HttpServletRequest request, HttpServletResponse response,
                                    @RequestParam(value = "ids", defaultValue = "") String ids, @RequestParam(value = "status", defaultValue = "0") int status) {

        if (StringUtil.isBlank(ids)) {
            return ResponseDTO.fail("请选择要更改状态的跟单内容！");
        }

        List<Integer> arrayIds = StringUtil.convertStrsToList(ids);
        documentaryContentService.updateStatusBatch(arrayIds, status, getMcStaffRealName());

        return ResponseDTO.success(true,"操作成功！");
    }

    @RequestMapping(value = "updateSort")
    @ResponseBody
    @RequiresPermissions("/orderconfig/documentarycontent/list.action")
    @SuppressWarnings("Duplicates")
    public ResponseDTO updateSort(HttpServletRequest request, HttpServletResponse response,
        @RequestParam(value = "id", defaultValue = "0") Integer id, @RequestParam(value = "sort", defaultValue = "0") Integer sort) {

        if (id == 0) {
            return ResponseDTO.fail("请选择要更改排序的跟单内容！");
        }
        if (sort == 0) {
            return ResponseDTO.fail("请设置跟单内容排序值！");
        }

        documentaryContentService.updateSortKey(id, sort, getMcStaffRealName());
        return ResponseDTO.success(true,"操作成功！");
    }

    @RequestMapping(value = "updateFirstLetter")
    @ResponseBody
    @RequiresPermissions("/orderconfig/documentarycontent/list.action")
    public ResponseDTO updateFirstLetter(HttpServletRequest request, HttpServletResponse response,
        @RequestParam(value = "id", defaultValue = "0") Integer id,
        @RequestParam(value = "firstLetter", defaultValue = "") String firstLetter) {

        if (id == 0) {
            return ResponseDTO.fail("请选择要更改首字母的跟单内容！");
        }
        if (firstLetter.isEmpty()) {
            return ResponseDTO.fail("请设置首字母！");
        }

        documentaryContentService.updateFirstLetterByKey(id, firstLetter, getMcStaffRealName());
        return ResponseDTO.success(true,"操作成功！");
    }

    @RequestMapping(value = "addEdit")
    @ResponseBody
    @RequiresPermissions("/orderconfig/documentarycontent/detail.action")
    public ResponseDTO addEdit(HttpServletRequest request, HttpServletResponse response,
        @RequestBody DocumentaryContentVO documentaryContentVO) {
        Date now = DateUtil.getNow();

        Integer id = documentaryContentVO.getId();

        try {
            if (id == null) {
                documentaryContentVO.setCreater(getMcStaffRealName());
                documentaryContentVO.setCreateTime(now);
                id = documentaryContentBService.insert(documentaryContentVO);
            } else {
                documentaryContentVO.setUpdater(getMcStaffRealName());
                documentaryContentVO.setUpdateTime(now);
                documentaryContentBService.update(documentaryContentVO);
            }

            return ResponseDTO.success(true,"操作成功！");
        } catch (org.springframework.dao.DuplicateKeyException ex) {
            logger.error("跟单内容【{}】已经存在，不能重复添加！", documentaryContentVO.getContent());
            return ResponseDTO.fail("跟单内容已经存在，不能重复添加！");
        } catch (Exception ex) {
            logger.error("[zmn]新增/修改跟单内容失败,category:{},ex:{}", documentaryContentVO, ex.getMessage(), ex);
            return ResponseDTO.fail("操作失败！");
        }
    }
}
